//
//  IGEmptyView.m
//  jingdongfang
//
//  Created by BOE on 2017/10/24.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGEmptyView.h"
#import "IGNoNetWorkVC.h"
#import "IGNetReachable.h"

@interface IGEmptyView()

#pragma mark -NoNet
@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) UIButton *checkNet;

#pragma mark -Empty
@property (nonatomic, strong) UIActivityIndicatorView *activityView;
@property (nonatomic, strong) UILabel *label3;                          ///>emptyTitle
@property (nonatomic, strong) UIImageView *imageView;                   ///>emptyImage

@end

@implementation IGEmptyView

- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 200)];
    if (self) {
//        self.title = @"暂无数据~";
        self.title = NSLocalizedString(@"No Data", nil);
        [self createUI];
    }
    return self;
}

- (void)createUI {
    
    if (![IGNetReachable isReachable]) {
        [self noNetView];
    } else {
        [self emptyView];
    }
}

- (void)emptyView {
    
    _imageView = [UIImageView new];
    [self addSubview:_imageView];
    CGSize size = CGSizeMake(0, 0);
    if (self.image) {
        _imageView.image = self.image;
        size = self.image.size;
    }
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.size.mas_equalTo(size);
    }];
    
    _label2 = [UILabel new];
    _label2.font = [UIFont font13];
    _label2.textColor = [UIColor c5Color];
    _label2.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_label2];
    
    [_label2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageView.mas_bottom).mas_offset(10);
        make.left.right.mas_equalTo(0);
        make.width.mas_equalTo(KSCREENWIDTH);
    }];
}

- (void)setImage:(UIImage *)image {
    _image = image;
    
    CGSize size = self.image.size;
    [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.size.mas_equalTo(size);
    }];
}

- (void)noNetView {
    _label1 = [UILabel new];
    _label1.font = [UIFont boldSystemFontOfSize:16];
    _label1.textColor = [UIColor c4Color];
    _label1.textAlignment = NSTextAlignmentCenter;
    _label1.text = NSLocalizedString(@"Current network not available.", nil);
    [self addSubview:_label1];
    
    _label2 = [UILabel new];
    _label2.font = [UIFont font13];
    _label2.textColor = [UIColor c5Color];
    _label2.textAlignment = NSTextAlignmentCenter;
    _label2.text = NSLocalizedString(@"Please check your network setting.", nil);
    [self addSubview:_label2];
    
    _checkNet = [UIButton buttonWithType:UIButtonTypeCustom];
    _checkNet.layer.borderColor = [UIColor c4Color].CGColor;
    _checkNet.layer.borderWidth = 1.0;
    _checkNet.layer.masksToBounds = YES;
    _checkNet.layer.cornerRadius = 15;
    _checkNet.titleLabel.font = [UIFont font16];
    [_checkNet setTitleColor:[UIColor c4Color] forState:UIControlStateNormal];
    [_checkNet setTitle:NSLocalizedString(@"See Details", nil) forState:UIControlStateNormal];
    [self addSubview:_checkNet];
    [_checkNet setTapActionWithBlock:^{
        [UIViewController ig_pushViewControllerClass:[IGNoNetWorkVC class] animated:YES];
    }];
    
    [_label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];
    
    [_label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(KSCREENWIDTH);
        make.top.mas_equalTo(_label1.mas_bottom).mas_offset(10);
        make.height.mas_equalTo(20);
    }];
    
    [_checkNet mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_label2.mas_bottom).mas_offset(10);
        make.height.mas_equalTo(30);
        make.centerX.mas_equalTo(_label2.mas_centerX);
        make.width.mas_equalTo(100);
    }];
}

- (void)setRequestApi:(IGBaseRequest *)requestApi {
    _requestApi = requestApi;
    
    if (![IGNetReachable isReachable]) {
        return;
    }
    
    if (requestApi.resultStatus == IGApiResultStatusStart) {
        [self addSubview:self.activityView];
        [self.activityView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.centerX.mas_equalTo(self.mas_centerX);
        }];
    } else if (requestApi.resultStatus == IGApiResultStatusSuccess) {
        self.label2.text = self.title;
        if (self.image) {
            self.imageView.image = self.image;
        }
        [self.activityView removeFromSuperview];
    } else if (requestApi.resultStatus == IGApiResultStatusLogicErr) {
        self.label2.text = NSLocalizedString(@"Business Processing Failed", nil);
        [self.activityView removeFromSuperview];
    } else if (requestApi.resultStatus == IGApiResultStatusServerErr) {
//        self.label2.text = @"请求超时~";
        self.label2.text = NSLocalizedString(@"Request timed out", nil);
        [self.activityView removeFromSuperview];
    }
}

- (UIActivityIndicatorView *)activityView {
    if (!_activityView) {
        _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [_activityView startAnimating];
    }
    return _activityView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
